from flask import jsonify, request, session, redirect, url_for, g

from models import db
from models.index import Collection, Comment, CommentLike
from views import news_bp


@news_bp.route('/collect', methods=['POST'])
def news_collect():
    if not g.user:
        return {
            'errno': 5002,
            'errmsg': '请先登录'
        }
    action = request.json.get('action')
    news_id = request.json.get('news_id')
    user_id = session.get('user_id')
    if action == 'do':
        collection = Collection()
        collection.news_id = news_id
        collection.user_id = user_id
        db.session.add(collection)
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "成功"
        }
        return jsonify(ret)
    elif action == 'undo':
        collection = db.session.query(Collection).filter(Collection.user_id == user_id,
                                                         Collection.news_id == news_id).first()
        if collection:
            db.session.delete(collection)
            db.session.commit()
            ret = {
                'errno': 0,
                'errmsg': '取消收藏成功'
            }
        else:
            ret = {
                'errno': 4101,
                'errmsg': '取消收藏失败'
            }
        return jsonify(ret)


@news_bp.route("/comment", methods=['POST'])
def news_comment():
    if not g.user:
        return {
            'errno': 4102,
            'errmsg': '请先登录'
        }
    # 1. 提取出用户评价时的数据
    content = request.json.get("content")
    news_id = request.json.get("news_id")
    user_id = session.get("user_id")
    parent_id = request.json.get("parent_id")

    # 2. 保存到数据库
    new_comment = Comment()
    new_comment.news_id = news_id
    new_comment.user_id = user_id
    new_comment.content = content
    new_comment.parent_id = parent_id
    db.session.add(new_comment)
    db.session.commit()
    ret = {
        'errno': 0,
        'errmsg': '成功'

    }
    return jsonify(ret)


@news_bp.route('/comment_like', methods=['POST'])
def news_comment_like():
    if not g.user:
        return {
            'errno': 4101,
            'errmsg': '请先登录'
        }
    action = request.json.get('action')
    comment_id = request.json.get('comment_id')
    user_id = session.get('user_id')
    if action == 'do':
        comment_like = CommentLike()
        comment_like.comment_id = comment_id
        comment_like.user_id = user_id
        db.session.add(comment_like)
        db.session.commit()
        ret = {
            'errno': 0,
            'errmsg': '点赞成功'
        }

    elif action == 'undo':
        comment_like = db.session.query(CommentLike).filter(CommentLike.comment_id == comment_id,
                                                            CommentLike.user_id == user_id).first()
        if comment_like:
            db.session.delete(comment_like)
            db.session.commit()

            ret = {
                'errno': 0,
                'errmsg': '取消点赞成功'
            }
        else:
            ret = {
                'errno': 5008,
                'errmsg': '取消点赞失败'
            }

    else:
        ret = {
            'errno': 5009,
            'errmsg': '非法请求'
        }
    return jsonify(ret)
